﻿using Credisoft.Models.Credisoft_BO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Credisoft.Models.Credisoft_DAO
{
    public class EconomicGroupDAO
    {
        public EconomicGroupBO getEconomicGroupDataById(int economicGroupId)
        {
            credisoftEntities bd = new credisoftEntities();

            grupoeconomico found = (from p in bd.grupoeconomico
                                    where p.idgrupo == economicGroupId
                                    select p).First();

            EconomicGroupBO objEconomicGroup = new EconomicGroupBO();

            if (found != null)
            {
                objEconomicGroup.credibility = found.credibilidad;
                objEconomicGroup.economicGroupId = found.idgrupo;
                objEconomicGroup.name = found.nombre;
            }
            return objEconomicGroup;
        }

        public List<EconomicGroupBO> getEconomicGroupDataList()
        {
            credisoftEntities bd = new credisoftEntities();


            var data = (from p in bd.grupoeconomico          
                        select p);

            List<EconomicGroupBO> listEconomicGroup = new List<EconomicGroupBO>();
            foreach (var result in data)
            {
                EconomicGroupBO economicGroupAux = new EconomicGroupBO();
                economicGroupAux.economicGroupId = result.idgrupo;
                economicGroupAux.name = result.nombre;
                economicGroupAux.credibility = result.credibilidad;

                listEconomicGroup.Add(economicGroupAux);
            }

            return listEconomicGroup;
        }

        public int insertEconomicGroup(grupoeconomico economicGroup)
        {
            int success = 0;
            credisoftEntities db = new credisoftEntities(); // Se abre la conexion con la Base de Datos

            var existe = from p in db.grupoeconomico
                         where p.nombre.Equals(economicGroup.nombre)
                         select p;

            if (existe.Count() > 0) return -1;

            db.grupoeconomico.Add(economicGroup); // Se inserta un nuevo elemento en la Base de Datos
            success = db.SaveChanges(); // Se realiza commit en la Base de Datos

            return success;
        }
    }
}